package com.arcgis.app.utils;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReference;

import java.util.Locale;

public class StringUtils {

    public static String geometryToStr(Geometry geometry) {


        String returnMsg = "长度: 0M,面积: 0㎡";

        if(geometry == null)return returnMsg;

        if(geometry.getGeometryType() == GeometryType.POINT){

            returnMsg = formatPoint((Point) geometry);
        }
        else if(geometry.getGeometryType() == GeometryType.POLYLINE
        ){

            returnMsg = "长度:";

            double len = GeometryEngine.length((Polyline) geometry);

            returnMsg += " "+formatLength(len);
        }

        else if(geometry.getGeometryType() == GeometryType.POLYGON){

            returnMsg="面积:";

            double area = GeometryEngine.area((Polygon) geometry);

            area = Math.abs(area);

            returnMsg += " "+ String.format(Locale.CHINA,"%.2f㎡", Math.floor(area * 100)/100);

        }

        return returnMsg;
    }

    public static String formatPoint(Point point){

        SpatialReference spatialReference = point.getSpatialReference();

        if(spatialReference != null && spatialReference.getWkid() != 4326){

            point = (Point) GeometryEngine.project(point, SpatialReference.create(4326));
        }

        return String.format(Locale.CHINA,"经度%.6f|纬度%.6f",point.getX(),point.getY());
    }

    public static String formatLength(double length){

        if(length <= 1000){

            return String.format(Locale.CHINA,"%.2fM", Math.floor(length*100)/100);
        }else{
            return String.format(Locale.CHINA,"%.2fKM", Math.floor(length/1000*100)/100);
        }
    }


    public static Boolean isNetworkURL(String url){
        if(url.contains(Urls.SEVER_URL)){
            return true;
        }
        return false;
    }
}
